package org.ubc.cs410.disposal2020;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Set;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.users.User;

/*
 * The Player Class that stores information about the player
 */

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Player {
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;
	@Persistent(mappedBy = "player",defaultFetchGroup="true")
	private List<Robot> robots;
	@Persistent
	private String user;
	@Persistent
	private boolean active;
	@Persistent(defaultFetchGroup="true")
	private Game game;
	@Persistent
	private int score;
	@Persistent(defaultFetchGroup="true")
	private Float factoryLat;
	@Persistent(defaultFetchGroup="true")
	private Float factoryLng;
	// Maximum number of robots a player can have.
	public static final int MAX_ROBOTS = 5;

	public Player(String user, Game game) {
		this.user = user;
		this.robots = new ArrayList<Robot>();
		this.game = game;		
		this.active = true;
		this.score = 0;

		Set<BuildingBoundary> buildings = game.getMap().getBuildings();
		BuildingBoundary factory = (BuildingBoundary) buildings.toArray()[new Random().nextInt(buildings.size())];
		this.factoryLat = factory.getLatitudes().get(0);
		this.factoryLng = factory.getLongitudes().get(0);

		for (int i=0;i<MAX_ROBOTS;i++) {
			this.robots.add(new Robot(this));
		}
	}

	public boolean isActive() {
		return this.active;
	}


	public List<Robot> getRobots() {
		return robots;
	}

	public Game getGame() {
		return game;
	}

	public void setGame(Game game) {
		this.game = game;
	}

	public Float getFactoryLat() {
		return this.factoryLat;
	}

	public Float getFactoryLng() {
		return this.factoryLng;
	}

	public String getUser() {
		return user;
	}

	public int getScore() {
		return score;
	}

	public void retire() {
		this.active = false; 
	}

	public Key getKey() {
		return this.key;
	}

	public int addScore(int collected) {
		this.score += collected;
		return this.score;
	}

}
